import { _decorator, Component, director, Label, Node, Tween, tween, Vec3 } from 'cc';
import { EffectManager } from '../AshFramework/Managers/EffectManager';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { GameManager } from '../GameManager';
import { NodeUtil } from '../AshFramework/Utils/NodeUtil';
import Banner from '../Banner';
import { UIManager } from '../AshFramework/Managers/UIManager';

const { ccclass, property } = _decorator;

@ccclass('WinPanel')
export class WinPanel extends Component {
    Money: Node;
    MoneyLabel: Label;
    CrossHair: Node;
    GetButton: Node;
    VideoGetButton: Node;
    VideoGetButtonLabel: Label;
    GetButtonLabel: Label;

    isGet: boolean = false;

    isWatchVideo: boolean = false;

    range = [[0, 75], [75, 145], [145, 220]];
    ratio: number = 1;
    len: number = 0;

    protected onLoad(): void {
        this.Money = NodeUtil.GetNode("Money", this.node);
        this.CrossHair = NodeUtil.GetNode("CrossHair", this.node);
        this.GetButton = NodeUtil.GetNode("GetButton", this.node);
        this.VideoGetButton = NodeUtil.GetNode("VideoGetButton", this.node);
        this.MoneyLabel = NodeUtil.GetComponent("MoneyLabel", this.node, Label);
        this.VideoGetButtonLabel = NodeUtil.GetComponent("VideoGetButtonLabel", this.node, Label);
        this.GetButtonLabel = NodeUtil.GetComponent("GetButtonLabel", this.node, Label);

        EventManager.on(MyEvent.REFRESH_MONEY, this.RefreshMoney, this);
    }

    RefreshMoney() {
        this.MoneyLabel.string = `${GameManager.Instance.Money}`;
    }

    Show() {
        Banner.Instance.ShowBannerAd();
        Banner.Instance.ShowCustomAd();
        this.isWatchVideo = false;
        this.node.setScale(0, 0, 0);
        Tween.stopAllByTarget(this.node);
        AudioManager.PlaySound(Audios.Win);
        tween(this.node).to(0.5, { scale: Vec3.ONE }, { easing: `backOut` }).call(() => {
            this.RefreshMoney();
            this.scheduleOnce(() => {
                this.GetButton.active = true;
            }, 1);
        }).start();
    }

    OnVideoGetButtonClick() {
        AudioManager.PlaySound(Audios.ButtonClick);
        this.isWatchVideo = true;
        Banner.Instance.ShowVideoAd(() => {
            EffectManager.MoneyEffect3(this.VideoGetButton.worldPosition, this.Money.worldPosition, this.node, 20, 0.2, 0.8, () => {
                GameManager.Instance.Lv += 1;
                GameManager.Instance.Money += 150 * this.ratio;

                director.loadScene("Start");
            });
        });
    }

    OnGetButtonClick() {
        AudioManager.PlaySound(Audios.ButtonClick);
        EffectManager.MoneyEffect3(this.GetButton.worldPosition, this.Money.worldPosition, this.node, 10, 0.2, 0.8, () => {
            GameManager.Instance.Lv += 1;
            GameManager.Instance.Money += 150;

            director.loadScene("Start");
        });
    }

    protected update(dt: number): void {
        if (this.isWatchVideo) return;
        this.len = this.CrossHair.position.length();
        this.ratio = 1;
        if (this.len < this.range[0][1] && this.len > this.range[0][0]) this.ratio = 5;
        if (this.len < this.range[1][1] && this.len > this.range[1][0]) this.ratio = 4;
        if (this.len < this.range[2][1] && this.len > this.range[2][0]) this.ratio = 3;
        if (this.VideoGetButtonLabel.string != `${this.ratio * 150}`) this.VideoGetButtonLabel.string = `${this.ratio * 150}`;
    }

}